package com.z_talk.client.core;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;

public class ClientSender {

    private Bootstrap bootstrap;
    private Channel channel;
    private static ClientSender clientSender = new ClientSender();

    public ClientSender() {
    }

    public static ClientSender getInstance(){
        return clientSender;
    }

    public void setBootstrap(Bootstrap bootstrap){
        this.bootstrap = bootstrap;
    }

    public void connect(String host, int port) throws InterruptedException {
        ChannelFuture channelFuture = bootstrap.connect(host, port).sync();
        channel = channelFuture.channel();
        System.out.println( "======"+channel.localAddress()+"======");
    }

    public void listen() throws InterruptedException {
        channel.closeFuture().sync();
    }

    public void sendMsg(Object msg) throws InterruptedException {
        channel.writeAndFlush(msg);
    }

}
